package com.zhou.yuefan.db;

import java.util.ArrayList;
import java.util.List;

import com.zhou.util.SqliteUtil;
import com.zhou.yuefan.model.Food;
import com.zhou.yuefan.model.Shop;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;



public class DBManager {
	private SQLiteDatabase db;
	public static final String DATABASE_NAME = "waimai"; // 保存的数据库文件名
	public static final String PACKAGE_NAME = "com.zhou.yuefan";
	public static final String DATABASE_PATH = "/data"
			+ Environment.getDataDirectory().getAbsolutePath() + "/"
			+ PACKAGE_NAME + "/databases"; // 在手机里存放数据库的位置
	public static final String DATABASE_FILENAME = DATABASE_PATH +"/"+ DATABASE_NAME;
	private Context context ;
	
	
	public DBManager(Context context){
		this.context = context;
		if (db==null||!db.isOpen()) {
			db = SqliteUtil.openDatabase(this.context, DATABASE_PATH, DATABASE_NAME);
		}
	}
	
	
	public void innitOrder(){
		String orderString = "update ";
	}
	
	public void setShopOrder(int id ,int order){
		String orderFmt = "update shops set shop_order = %s where _id = %s;";
		String updateOrder = String.format(orderFmt, order,id);
		db.execSQL(updateOrder);
	}
	
	
	
	
	public  SQLiteDatabase openDatabase(){
		
		return db;
		
	}
	
	private SQLiteDatabase createDatabase(Context context,String dbPath , String dbFileName){
		return openDatabase();
	}
	public void createDatabase(){
		createDatabase(this.context, DATABASE_PATH, DATABASE_NAME);
	}
	
	public List<Shop> getShopList(){
		List<Shop> shopList = new ArrayList<Shop>();
		String selectShops = "select * from shops ";
		Cursor cursor = db.rawQuery(selectShops, null);
		if (cursor!=null) {
			while(cursor.moveToNext()){
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				String name = cursor.getString(cursor.getColumnIndex("name"));
				Shop shop= new Shop();
				shop.setId(id);
				shop.setName(name);
				shopList.add(shop);
			}
			cursor.close();	
		}
		return shopList;
	}
	
	
	public List<Food> getFoodList(int shopId) {
		List<Food> foodList = new ArrayList<Food>();
		String seleString = " select * from shopswithfoods where shop_id=%s ";
		String selection = String.format(seleString, shopId);
		Log.i("Stringformat", selection);
		Cursor cursor = db.rawQuery(selection, null);
		System.out.println(cursor.toString());
		if (cursor != null) {
			while (cursor.moveToNext()) {
				int id = cursor.getInt(cursor.getColumnIndex("_id"));
				int shop_id = cursor.getInt(cursor.getColumnIndex("shop_id"));
				int food_id = cursor.getInt(cursor.getColumnIndex("food_id"));
				String shop_name = cursor.getString(cursor
						.getColumnIndex("shop_name"));
				String food_name = cursor.getString(cursor
						.getColumnIndex("food_name"));
				float price = cursor.getFloat(cursor.getColumnIndex("price"));
				int order = cursor.getInt(cursor.getColumnIndex("order"));
				String phone1 = cursor.getString(cursor
						.getColumnIndex("phone1"));
				String phone2 = cursor.getString(cursor
						.getColumnIndex("phone2"));
				String logo_src = cursor.getString(cursor
						.getColumnIndex("logo_src"));
				String banner_src = cursor.getString(cursor
						.getColumnIndex("banner_src"));
				String address = cursor.getString(cursor
						.getColumnIndex("address"));
				int isSms = cursor.getInt(cursor.getColumnIndex("issms"));
				String description = cursor.getString(cursor
						.getColumnIndex("description"));
				int state = cursor.getInt(cursor.getColumnIndex("state"));
				Shop shop = new Shop();
				shop.setId(shop_id);
				shop.setName(shop_name);
				shop.setPhone1(phone1);
				shop.setPhone2(phone2);
				shop.setLogo_src(logo_src);
				shop.setBanner_src(banner_src);
				shop.setAddress(address);
				shop.setIsSms(isSms);
				shop.setDescription(description);
				shop.setState(state);

				Food food = new Food(id, food_id, food_name, price, order, shop);
				foodList.add(food);
			}
			cursor.close();
		}
		return foodList;
	}
	public void closeDB(){
		db.close();
	}
}
